#include "luat_debug.h"
#include "luat_rtos.h"
#include "luat_mobile.h"
#include "luat_gpio.h"
#include "platform_define.h"
#include "gpio_dec.h"
#include "network_service.h"

//是否连接上网络
static uint8_t g_s_is_link_up;
//任务task
static luat_rtos_task_handle task_net_led_handle;
static luat_rtos_semaphore_t g_led_task_semaphore_handle;

//获取网络状态
uint8_t network_service_is_ready(void)
{
	return g_s_is_link_up;
}

//sim卡状态
static uint8_t g_s_sim_isready = 0;
//网络网络状态
static uint8_t g_s_net_status = 0;

//
static void mobile_event_cb(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status)
{
	switch(event)
	{
	case LUAT_MOBILE_EVENT_CFUN:
		LUAT_DEBUG_PRINT("cfun event，status %d", status);
		break;
	case LUAT_MOBILE_EVENT_SIM:
		switch(status)
		{
		case LUAT_MOBILE_SIM_READY:
			g_s_sim_isready = 1;
			LUAT_DEBUG_PRINT("sim event ready");
			break;
		case LUAT_MOBILE_NO_SIM:
			g_s_sim_isready = 0;
			LUAT_DEBUG_PRINT("sim event not insert");
			break;
		case LUAT_MOBILE_SIM_NEED_PIN:
			LUAT_DEBUG_PRINT("sim event need pin");
			break;
		}
		break;
	case LUAT_MOBILE_EVENT_REGISTER_STATUS:
		g_s_net_status = status;
		LUAT_DEBUG_PRINT("register event, status %d", status);
		break;
	case LUAT_MOBILE_EVENT_CELL_INFO:
		switch(status)
		{
		case LUAT_MOBILE_CELL_INFO_UPDATE:
			LUAT_DEBUG_PRINT("周期性搜索小区信息完成一次\r\n");
			break;
		case LUAT_MOBILE_SIGNAL_UPDATE:
			break;
		}
		break;
	case LUAT_MOBILE_EVENT_PDP:
        LUAT_DEBUG_PRINT("pdp event, cid %d, status %d", index, status);
		break;
	case LUAT_MOBILE_EVENT_NETIF:
		switch (status)
		{
		case LUAT_MOBILE_NETIF_LINK_ON:
			// LUAT_DEBUG_PRINT("netif link on event");
			LUAT_DEBUG_PRINT("网络注册成功\r\n");
			g_s_is_link_up = 1;
			break;
		default:
			// LUAT_DEBUG_PRINT("netif link off event");
			LUAT_DEBUG_PRINT("网络未注册成功\r\n");
			g_s_is_link_up = 0;
			break;
		}
		break;
	case LUAT_MOBILE_EVENT_TIME_SYNC:
		LUAT_DEBUG_PRINT("time sync event");
		break;
	case LUAT_MOBILE_EVENT_CSCON:
		LUAT_DEBUG_PRINT("rrc event, status %d", status);
		break;
	default:
		break;
	}
}
void net_led_send_light(void) {
	// luat_rtos_event_send(task_net_led_handle, 0, 0, 0, 0, 0);
	LUAT_DEBUG_PRINT("net_led_send_light---");
	luat_rtos_semaphore_release(g_led_task_semaphore_handle);
}
//控制NET指示灯闪烁
static void task_net_led_run(void *param)
{
	luat_gpio_cfg_t gpio_cfg;
	luat_gpio_set_default_cfg(&gpio_cfg);

	gpio_cfg.pin = Net_led_Pin;
	gpio_cfg.alt_fun=Net_led_Alt;
	luat_gpio_open(&gpio_cfg);

	// luat_event_t event = {0};
	while(1)
	{
		luat_rtos_semaphore_take(g_led_task_semaphore_handle, LUAT_WAIT_FOREVER);
		// LUAT_DEBUG_PRINT("TEST STATUS g_s_sim_isready: %d, g_s_net_status: %d, g_s_is_link_up: %d", g_s_sim_isready, g_s_net_status, g_s_is_link_up);
		// 如果有事件过来灯就亮一下。表示有定位坐标发送出去 用法： luat_rtos_event_send(task_net_led_handle, 0, 0, 0, 0, 0);
		// if (0 == luat_rtos_event_recv(task_net_led_handle, 0, &event, NULL, LUAT_WAIT_FOREVER) && g_s_is_link_up)
		if (g_s_is_link_up)
		{
			luat_gpio_set(Net_led_Pin, 1);
			luat_rtos_task_sleep(100);
			luat_gpio_set(Net_led_Pin, 0); 
		}
		 
	}
}
 
void network_service_init(void)
{
	luat_rtos_semaphore_create(&g_led_task_semaphore_handle, 1);
	//回调注册
	luat_mobile_set_sim_id(2);
	// luat_mobile_set_sim_detect_sim0_first();// 优先使用sim0
	luat_mobile_event_register_handler(mobile_event_cb); 
	luat_rtos_task_create(&task_net_led_handle, 1024, 30, "net_led", task_net_led_run, NULL, 32);
}
